enum StorageType {
  local = 'local',
  session = 'session'
}

class MaStorage {
  storage: Storage
  constructor(type: string) {
    this.storage = StorageType.local === type ? localStorage : sessionStorage
  }
  getCache(key: string) {
    const value = this.storage.getItem(key)
    if (value) {
      return JSON.parse(value)
    }
  }
  setCache(key: string, value: any) {
    return this.storage.setItem(key, JSON.stringify(value || ''))
  }
  deleteCache(key: string) {
    return this.storage.removeItem(key)
  }
  clearCache() {
    return this.storage.clear()
  }
}

const localCache = new MaStorage('local')
const sessionCache = new MaStorage('session')

export { localCache, sessionCache }
